﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
//***
using AutoMapper;
using AutoMapper.Configuration;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.ServiceLocation;

using OpenWeb.Dal.Interfaces;
using OpenWeb.Dal.Repository;
using OpenWeb.Dal.Models;
using OpenWeb.Dal.Services;
using OpenWeb.Dal.Mappers;
using System.Configuration;

namespace OpenWeb.Site
{
    // Nota: per istruzioni su come abilitare la modalità classica di IIS6 o IIS7, 
    // visitare il sito Web all'indirizzo http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            //var ConnStringName = ConfigurationManager.ConnectionStrings[0].Name;
            //var ConnStringName2 = ConfigurationManager.ConnectionStrings[1].Name;

            new OpenWeb.Dal.DbCreation.DbInitializer()
                .Init(OpenWeb.Dal.DbCreation.CreationEnum.CreateOnlyIfModelChange);

            //ConfigureContainer();// repository

            //new AutoMapperConfiguration().Configure(); //mapper
            //Mapper.AssertConfigurationIsValid();


        }

        public override void Init()
        {
            base.Init();
        }

        protected void Application_BeginRequest()
        {

        }


        #region other personal methods

        private void CreateMap()
        {
            // Mapper.CreateMap<A, B>().ForMember(    //b = Mapper.Map<A, B>(a);
        }
        
        private void ConfigureContainer()
        {
            //_container = new UnityContainer()
            //        .RegisterType<IContactsService, ContactService>()
            //  //.RegisterType<IFileService, FileService>()
            //        .RegisterType<IDocumentRepository, DocumentRepository>();


            //.RegisterType<IDocumentService, DocumentService>(new ContainerControlledLifetimeManager())
            //.RegisterType<IFileService, FileService>()
            //.RegisterType<IDocumentRepository, DocumentRepository>()



            //.RegisterType<IFileRepository, FileRepository>()
            //.RegisterType<IRepositoryContext, DmsRepositoryContext>()
            //.RegisterType<IFormsAuthenticationService, FormsAuthenticationService>()
            //.RegisterType<IMembershipService, AccountMembershipService>();

            //Set for Controller Factory
            //IControllerFactory controllerFactory = new UnityControllerFactory(_container);

            //ControllerBuilder.Current.SetControllerFactory(controllerFactory);

        }

        #endregion


    }
}